package com.xiong.ai.strategy;

import com.xiong.ai.exception.BusinessException;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @program: ai
 * @description:
 * @author: Mr.熊
 * @create: 2024-05-31 11:26
 **/
@Component
public class WordStrategy implements FileStrategy {

    protected final Logger LOGGER = LoggerFactory.getLogger(this.getClass());

    @Override
    public String doAnalyze(MultipartFile file) {
        String content = null;
        LOGGER.info("word文件解析开始");
        try {
            XWPFDocument document = new XWPFDocument(file.getInputStream());
            XWPFWordExtractor extractor = new XWPFWordExtractor(document);
            content = extractor.getText();
            extractor.close();
            document.close();
        } catch (IOException e) {
            throw new BusinessException("文件读取失败");
        }
        if (content == null) {
            throw new BusinessException("文件读取失败");
        }
        LOGGER.info("word文件解析完成");
        return content;
    }
}
